package com.itheima.googleplay9.ui.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.text.format.Formatter;
import android.view.MenuItem;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RatingBar;
import android.widget.ScrollView;
import android.widget.TextView;

import com.itheima.googleplay9.R;
import com.itheima.googleplay9.api.DataLoader;
import com.itheima.googleplay9.api.Url;
import com.itheima.googleplay9.bean.AppInfo;
import com.itheima.googleplay9.holder.AppDesHolder;
import com.itheima.googleplay9.holder.AppDownloadHolder;
import com.itheima.googleplay9.holder.AppInfoHolder;
import com.itheima.googleplay9.holder.AppSafeHolder;
import com.itheima.googleplay9.holder.AppScreenHolder;
import com.itheima.googleplay9.ui.view.LoadingPage;
import com.itheima.googleplay9.util.Utils;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by lxj on 2016/5/27.
 */
public class AppDetailActivity extends AppCompatActivity {
    @Bind(R.id.holder_container)
    LinearLayout holder_container;//holder的容器
    @Bind(R.id.scrollView)
    ScrollView scrollView;
    @Bind(R.id.download_container)
    FrameLayout download_container;


    private AppInfoHolder appInfoHolder;
    private AppSafeHolder appSafeHolder;
    private AppScreenHolder appScreenHolder;
    private AppDesHolder appDesHolder;
    private AppDownloadHolder appDownloadHolder;

    private String packageName;
    private AppInfo appInfo;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //1.获取Intent数据
        packageName = getIntent().getStringExtra("packageName");

        //2.设置ActionBar
        initActionBar();

        //3.由于Activity的界面加载也是有3种状态，而LoadingPage有管理界面加载的功能
        //所以我们和Fragment一样，让LoadingPage作为Activity的内容View
        LoadingPage loadingPage = new LoadingPage(this) {
            @Override
            protected Object onLoad() {
                return AppDetailActivity.this.onLoad();
            }

            @Override
            protected View createSuccessView() {
                View view = View.inflate(AppDetailActivity.this, R.layout.activity_app_detail, null);
                return view;
            }
        };
        setContentView(loadingPage);
        ButterKnife.bind(this);

        //4.初始化Holder
        initHolder();
    }

    /**
     * 初始化Holder
     */
    private void initHolder() {
        //1.添加app info模块
        appInfoHolder = new AppInfoHolder();
        holder_container.addView(appInfoHolder.getHolderView());

        //2.添加app safe模块
        appSafeHolder = new AppSafeHolder();
        holder_container.addView(appSafeHolder.getHolderView());

        //3.添加app screen模块
        appScreenHolder = new AppScreenHolder();
        holder_container.addView(appScreenHolder.getHolderView());

        //4.添加app des模块
        appDesHolder = new AppDesHolder();
        holder_container.addView(appDesHolder.getHolderView());
        //将scrollView传给appDesHolder
        appDesHolder.setScrollView(scrollView);

        //5.添加app download模块
        appDownloadHolder = new AppDownloadHolder();
        download_container.addView(appDownloadHolder.getHolderView());
    }

    /**
     * 加载数据
     *
     * @return
     */
    private Object onLoad() {
        String url = String.format(Url.AppDetail, packageName);
        appInfo = DataLoader.getInstance().loadBeanData(url, AppInfo.class);
        if (appInfo != null) {
            //更新UI
            Utils.runOnUIThread(new Runnable() {
                @Override
                public void run() {
                    updateUI();
                }
            });
        }
        return appInfo;
    }

    /**
     * 更新各个模块的UI
     */
    private void updateUI() {
        //1.绑定app info模块的数据
        appInfoHolder.bindData(appInfo);
        //2.绑定app safe模块的数据
        appSafeHolder.bindData(appInfo);
        //3.绑定app screen模块的数据
        appScreenHolder.bindData(appInfo);
        //4.绑定app des模块的数据
        appDesHolder.bindData(appInfo);
        //5.绑定app download模块的数据
        appDownloadHolder.bindData(appInfo);
    }

    /**
     * 设置ActionBar
     */
    private void initActionBar() {
        ActionBar actionBar = getSupportActionBar();
        actionBar.setTitle(getString(R.string.app_detail));

        //显示和启用Home按钮
        actionBar.setDisplayShowHomeEnabled(true);
        actionBar.setDisplayHomeAsUpEnabled(true);
    }

    /**
     * 点击ActionBar的home按钮的时候会执行该方法
     *
     * @param item
     * @return
     */
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        //判断如果点击的是home按钮才推出
        if (item.getItemId() == android.R.id.home) {
            //推出当前界面
            finish();
        }
        return super.onOptionsItemSelected(item);
    }
}
